LanguageExt.Core

LanguageExt.Core Effects IO

Contents

record EnvIO ( CancellationToken Token, CancellationTokenSource Source, SynchronizationContext? SyncContext) Source #

Environment for the IO monad

Properties

property EnvIO LocalCancel Source #

property EnvIO LocalSyncContext Source #

Methods

method EnvIO New ( CancellationToken token, CancellationTokenSource source, SynchronizationContext? syncContext) Source #

method EnvIO New () Source #

method EnvIO New (CancellationTokenSource src) Source #

method void Dispose () Source #

method string ToString () Source #

record ForkIO <A> (IO<Unit> Cancel, IO<A> Await) Source #

Result of forking an IO monad

Parameters

type A

Bound value type

param Cancel

An IO monad, which if invoked, would cancel the forked IO operation

param Await

An IO monad, which if invoked, would await the result of the forked IO operation. Obviously, this mitigates the reasons for forking somewhat, but this struct could be passed to another process that does the awaiting - and so still has some value.

record IO <A> (Func<EnvIO, A> runIO) Source #

A value of type IO a is a computation which, when performed, does some I/O before returning a value of type A.

There is really only one way you should "perform" an I/O action: bind it to Main in your program: When your program is run, the I/O will be performed. It shouldn't be possible to perform I/O from an arbitrary function, unless that function is itself in the IO monad and called at some point, directly or indirectly, from Main.

Obviously, as this is C#, the above restrictions are for you to enforce. It would be reasonable to relax that approach and have I/O invoked from, say, web-request handlers - or any other 'edges' of your application.

IO is a monad, so IO actions can be combined using either the LINQ-notation or the bind operations from the Monad class.

Parameters

type A

Bound value

param runIO

The lifted thunk that is the IO operation

Methods

method IO<A> Pure (A value) Source #

method IO<A> Fail (Error value) Source #

method IO<A> Lift (Func<A> f) Source #

method IO<A> Lift (Func<EnvIO, A> f) Source #

method IO<A> LiftAsync (Func<ValueTask<A>> f) Source #

method IO<A> LiftAsync (Func<EnvIO, ValueTask<A>> f) Source #

method IO<B> Map <B> (Func<A, B> f) Source #

method IO<A> MapFail (Func<Error, Error> f) Source #

method IO<B> BiMap <B> (Func<A, B> Succ, Func<Error, Error> Fail) Source #

method IO<B> Match <B> (Func<A, B> Succ, Func<Error, B> Fail) Source #

method IO<A> IfFail (Func<Error, A> Fail) Source #

method IO<A> IfFail (A Fail) Source #

method IO<S> Fold <S> ( Schedule schedule, S initialState, Func<S, A, S> folder) Source #

method IO<S> Fold <S> ( S initialState, Func<S, A, S> folder) Source #

method IO<S> FoldWhile <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldWhile <S> ( S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldWhile <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldWhile <S> ( S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldWhile <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<S> FoldWhile <S> ( S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<S> FoldUntil <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldUntil <S> ( S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldUntil <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldUntil <S> ( S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldUntil <S> ( S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<S> FoldUntil <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<A> Post () Source #

Make this IO computation run on the SynchronizationContext that was captured at the start of the IO chain (i.e. the one embedded within the EnvIO environment that is passed through all IO computations)

method IO<B> Bind <B> (Func<A, IO<B>> f) Source #

method IO<B> Bind <B> (Func<A, K<IO, B>> f) Source #

method IO<B> Bind <B> (Func<A, Pure<B>> f) Source #

method IO<B> Select <B> (Func<A, B> f) Source #

method IO<C> SelectMany <B, C> (Func<A, IO<B>> bind, Func<A, B, C> project) Source #

method IO<C> SelectMany <B, C> (Func<A, Pure<B>> bind, Func<A, B, C> project) Source #

method OptionT<M, C> SelectMany <M, B, C> (Func<A, OptionT<M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>, Alternative<M>

method TryT<M, C> SelectMany <M, B, C> (Func<A, TryT<M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>, Alternative<M>

method EitherT<L, M, C> SelectMany <L, M, B, C> (Func<A, EitherT<L, M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>, Alternative<M>

method ValidationT<F, M, C> SelectMany <F, M, B, C> (Func<A, ValidationT<F, M, B>> bind, Func<A, B, C> project) Source #

where F : Monoid<F>
where M : Monad<M>, Alternative<M>

method ReaderT<Env, M, C> SelectMany <Env, M, B, C> (Func<A, ReaderT<Env, M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>, Alternative<M>

method StateT<S, M, C> SelectMany <S, M, B, C> (Func<A, StateT<S, M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>, Alternative<M>

method ResourceT<M, C> SelectMany <M, B, C> (Func<A, ResourceT<M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>, Alternative<M>

method Eff<C> SelectMany <B, C> (Func<A, Eff<B>> bind, Func<A, B, C> project) Source #

method Eff<RT, C> SelectMany <RT, B, C> (Func<A, Eff<RT, B>> bind, Func<A, B, C> project) Source #

method IO<C> SelectMany <C> (Func<A, Guard<Error, Unit>> bind, Func<A, Unit, C> project) Source #

method IO<A> Or (IO<A> mb) Source #

method IO<A> Timeout (TimeSpan timeout) Source #

Applies a time limit to the IO computation. If exceeded an exception is thrown.

Parameters

param timeout

Timeout

returns

Result of the operation or throws if the time limit exceeded.

method IO<ForkIO<A>> Fork (Option<TimeSpan> timeout = default) Source #

Queues the specified work to run on the thread pool

Parameters

param timeout

Optional timeout

returns

Fork record that contains members for cancellation and optional awaiting

method A Run () Source #

Run the IO monad to get its result

Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued operations to run concurrently. So, even though this call isn't awaitable it still plays nicely and doesn't block the thread.

NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into other error handling monads to leverage more declarative error handling.

Parameters

returns

Result of the IO operation

method A Run (EnvIO env) Source #

Run the IO monad to get its result

Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued operations to run concurrently. So, even though this call isn't awaitable it still plays nicely and doesn't block the thread.

NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into other error handling monads to leverage more declarative error handling.

Parameters

param env

IO environment

returns

Result of the IO operation

method IO<A> Repeat () Source #

Keeps repeating the computation forever, or until an error occurs

Parameters

returns

The result of the last invocation

method IO<A> Repeat (Schedule schedule) Source #

Keeps repeating the computation, until the scheduler expires, or an error occurs

Parameters

param schedule

Scheduler strategy for repeating

returns

The result of the last invocation

method IO<A> RepeatWhile (Func<A, bool> predicate) Source #

Keeps repeating the computation until the predicate returns false, or an error occurs

Parameters

param predicate

Keep repeating while this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> RepeatWhile ( Schedule schedule, Func<A, bool> predicate) Source #

Keeps repeating the computation, until the scheduler expires, or the predicate returns false, or an error occurs

Parameters

param schedule

Scheduler strategy for repeating

param predicate

Keep repeating while this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> RepeatUntil ( Func<A, bool> predicate) Source #

Keeps repeating the computation until the predicate returns true, or an error occurs

Parameters

param predicate

Keep repeating until this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> RepeatUntil ( Schedule schedule, Func<A, bool> predicate) Source #

Keeps repeating the computation, until the scheduler expires, or the predicate returns true, or an error occurs

Parameters

param schedule

Scheduler strategy for repeating

param predicate

Keep repeating until this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> Retry () Source #

Retry if the IO computation fails

This variant will retry forever

method IO<A> Retry (Schedule schedule) Source #

Retry if the IO computation fails

This variant will retry until the schedule expires

method IO<A> RetryWhile (Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying whilst the predicate returns true for the error generated at each iteration; at which point the last raised error will be thrown.

method IO<A> RetryWhile ( Schedule schedule, Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying whilst the predicate returns true for the error generated at each iteration; or, until the schedule expires; at which point the last raised error will be thrown.

method IO<A> RetryUntil ( Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying until the predicate returns true for the error generated at each iteration; at which point the last raised error will be thrown.

method IO<A> RetryUntil ( Schedule schedule, Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying until the predicate returns true for the error generated at each iteration; or, until the schedule expires; at which point the last raised error will be thrown.

method string ToString () Source #

Operators

operator | (IO<A> ma, IO<A> mb) Source #

operator | (IO<A> ma, Pure<A> mb) Source #

operator | (IO<A> ma, Fail<Error> mb) Source #

operator | (IO<A> ma, Fail<Exception> mb) Source #

operator | (IO<A> ma, Error mb) Source #

operator | (IO<A> ma, CatchError mb) Source #

operator | (IO<A> ma, CatchValue<A> mb) Source #

operator | (IO<A> ma, CatchIO<A> mb) Source #

operator | (IO<A> ma, CatchM<IO, A> mb) Source #

operator | (IO<A> ma, CatchError<Error> mb) Source #

operator | (IO<A> ma, CatchError<Exception> mb) Source #

operator | (IO<A> ma, CatchValue<Error, A> mb) Source #

operator | (IO<A> ma, CatchValue<Exception, A> mb) Source #

class IOExtensions Source #

Methods

method IO<A> As <A> (this K<IO, A> ma) Source #

Convert the kind version of the IO monad to an IO monad.

This is a simple cast operation which is just a bit more elegant than manually casting.

Parameters

type A
param ma
returns

method IO<A> Flatten <A> (this Task<IO<A>> tma) Source #

Get the outer task and wrap it up in a new IO within the IO

method IO<A> Flatten <A> (this IO<IO<A>> mma) Source #

Unwrap the inner IO to flatten the structure

method IO<A> Await <A> (this K<IO, ForkIO<A>> ma) Source #

Await a forked operation

method K<M, ForkIO<A>> Fork <M, A> (this K<M, A> ma, Option<TimeSpan> timeout = default) Source #

where M : Monad<M>

Queue this IO operation to run on the thread-pool.

Parameters

param timeout

Maximum time that the forked IO operation can run for. None for no timeout.

returns

Returns a ForkIO data-structure that contains two IO effects that can be used to either cancel the forked IO operation or to await the result of it.

method IO<B> Apply <A, B> (this IO<Func<A, B>> ff, IO<A> fa) Source #

method IO<B> Action <A, B> (this IO<A> fa, IO<B> fb) Source #

class IO Source #

Fields

field IO<EnvIO> env = lift(e => e) Source #

field IO<CancellationToken> token = lift(e => e.Token) Source #

field IO<CancellationTokenSource> source = lift(e => e.Source) Source #

field IO<Option<SynchronizationContext>> syncContext = lift(e => Optional(e.SyncContext)) Source #

Methods

method IO<A> Fail <A> (Error value) Source #

Put the IO into a failure state

Parameters

type A

Bound value type

param value

Error value

returns

IO in a failed state. Always yields an error.

method IO<Unit> lift (Action f) Source #

method IO<A> local <A> (K<IO, A> ma) Source #

Creates a local cancellation environment

A local cancellation environment stops other IO computations, that rely on the same environmental cancellation token, from being taken down by a regional cancellation.

If a IO.cancel is invoked locally then it will still create an exception that propagates upwards and so catching cancellations is still important.

Parameters

type A

Bound value

param ma

Computation to run within the local context

returns

Result of the computation

method IO<A> lift <A> (Either<Error, A> ma) Source #

method IO<A> lift <A> (Fin<A> ma) Source #

method IO<A> lift <A> (Func<A> f) Source #

method IO<A> lift <A> (Func<EnvIO, A> f) Source #

method IO<A> lift <A> (Func<Fin<A>> f) Source #

method IO<A> lift <A> (Func<EnvIO, Fin<A>> f) Source #

method IO<A> lift <A> (Func<Either<Error, A>> f) Source #

method IO<A> lift <A> (Func<EnvIO, Either<Error, A>> f) Source #

method IO<A> liftAsync <A> (Func<ValueTask<A>> f) Source #

method IO<A> liftAsync <A> (Func<EnvIO, ValueTask<A>> f) Source #

method IO<B> bind <A, B> (K<IO, A> ma, Func<A, K<IO, B>> f) Source #

method IO<B> map <A, B> (Func<A, B> f, K<IO, A> ma) Source #

method IO<B> map <A, B> (Func<A, B> f, IO<A> ma) Source #

method IO<B> apply <A, B> (K<IO, Func<A, B>> mf, K<IO, A> ma) Source #

method IO<B> action <A, B> (K<IO, A> ma, K<IO, B> mb) Source #

method IO<A> empty <A> () Source #

method IO<A> or <A> (K<IO, A> ma, K<IO, A> mb) Source #

method IO<ForkIO<A>> fork <A> (K<IO, A> ma, Option<TimeSpan> timeout = default) Source #

Queue this IO operation to run on the thread-pool.

Parameters

param timeout

Maximum time that the forked IO operation can run for. None for no timeout.

returns

Returns a ForkIO data-structure that contains two IO effects that can be used to either cancel the forked IO operation or to await the result of it.

method IO<Unit> yield (double milliseconds) Source #

Yield the thread for the specified milliseconds or until cancelled.

Parameters

param milliseconds

Amount of time to yield for

returns

Unit

class IO Source #

Methods

method IO<A> Pure <A> (A value) Source #

class Prelude Source #

Fields

field IO<CancellationToken> cancelToken = IO.lift(e => e.Token) Source #

Access the cancellation-token from the IO environment

Parameters

returns

CancellationToken

field IO<Unit> cancel = new (e => { e.Source.Cancel(); throw new TaskCanceledException(); }) Source #

Request a cancellation of the IO expression

field IO<Unit> unitIO = IO<Unit>.Pure(default) Source #

Always yields a Unit value

field IO<EnvIO> envIO = IO<EnvIO>.Lift(e => e) Source #

Yields the IO environment

Methods

method IO<A> local <A> (K<IO, A> ma) Source #

Creates a local cancellation environment

A local cancellation environment stops other IO computations, that rely on the same environmental cancellation token, from being taken down by a regional cancellation.

If a IO.cancel is invoked locally then it will still create an exception that propagates upwards and so catching cancellations is still important.

Parameters

type A

Bound value

param ma

Computation to run within the local context

returns

Result of the computation

method K<M, ForkIO<A>> fork <M, A> (K<M, A> ma, Option<TimeSpan> timeout = default) Source #

where M : Monad<M>

Queue this IO operation to run on the thread-pool.

Parameters

param timeout

Maximum time that the forked IO operation can run for. None for no timeout.

returns

Returns a ForkIO data-structure that contains two IO effects that can be used to either cancel the forked IO operation or to await the result of it.

method IO<A> timeout <A> (TimeSpan timeout, K<IO, A> ma) Source #

Timeout operation if it takes too long

method IO<A> repeat <A> (K<IO, A> ma) Source #

Keeps repeating the computation

Parameters

type A

Computation bound value type

param ma

Computation to repeat

returns

The result of the last invocation of ma

method IO<A> repeat <A> (Schedule schedule, K<IO, A> ma) Source #

Keeps repeating the computation, until the scheduler expires

Parameters

type A

Computation bound value type

param schedule

Scheduler strategy for repeating

param ma

Computation to repeat

returns

The result of the last invocation of ma

method IO<A> repeatWhile <A> (K<IO, A> ma, Func<A, bool> predicate) Source #

Keeps repeating the computation until the predicate returns false

Parameters

type A

Computation bound value type

param ma

Computation to repeat

returns

The result of the last invocation of ma

method IO<A> repeatWhile <A> ( Schedule schedule, K<IO, A> ma, Func<A, bool> predicate) Source #

Keeps repeating the computation, until the scheduler expires, or the predicate returns false

Parameters

type A

Computation bound value type

param schedule

Scheduler strategy for repeating

param ma

Computation to repeat

returns

The result of the last invocation of ma

method IO<A> repeatUntil <A> ( K<IO, A> ma, Func<A, bool> predicate) Source #

Keeps repeating the computation until the predicate returns true

Parameters

type A

Computation bound value type

param ma

Computation to repeat

returns

The result of the last invocation of ma

method IO<A> repeatUntil <A> ( Schedule schedule, K<IO, A> ma, Func<A, bool> predicate) Source #

Keeps repeating the computation, until the scheduler expires, or the predicate returns true

Parameters

type A

Computation bound value type

param schedule

Scheduler strategy for repeating

param ma

Computation to repeat

returns

The result of the last invocation of ma

method IO<A> retry <A> (K<IO, A> ma) Source #

Keeps retrying the computation

Parameters

type A

Computation bound value type

param ma

Computation to retry

returns

The result of the last invocation of ma

method IO<A> retry <A> (Schedule schedule, K<IO, A> ma) Source #

Keeps retrying the computation, until the scheduler expires

Parameters

type A

Computation bound value type

param schedule

Scheduler strategy for retrying

param ma

Computation to retry

returns

The result of the last invocation of ma

method IO<A> retryWhile <A> ( K<IO, A> ma, Func<Error, bool> predicate) Source #

Keeps retrying the computation until the predicate returns false

Parameters

type A

Computation bound value type

param ma

Computation to retry

returns

The result of the last invocation of ma

method IO<A> retryWhile <A> ( Schedule schedule, K<IO, A> ma, Func<Error, bool> predicate) Source #

Keeps retrying the computation, until the scheduler expires, or the predicate returns false

Parameters

type A

Computation bound value type

param schedule

Scheduler strategy for retrying

param ma

Computation to retry

returns

The result of the last invocation of ma

method IO<A> retryUntil <A> ( K<IO, A> ma, Func<Error, bool> predicate) Source #

Keeps retrying the computation until the predicate returns true

Parameters

type A

Computation bound value type

param ma

Computation to retry

returns

The result of the last invocation of ma

method IO<A> retryUntil <A> ( Schedule schedule, K<IO, A> ma, Func<Error, bool> predicate) Source #

Keeps retrying the computation, until the scheduler expires, or the predicate returns true

Parameters

type A

Computation bound value type

param schedule

Scheduler strategy for retrying

param ma

Computation to retry

returns

The result of the last invocation of ma

method IO<Unit> yield (double milliseconds) Source #

Yield the thread for the specified milliseconds or until cancelled.

Parameters

param milliseconds

Amount of time to yield for

returns

Unit